package com.mc.search;

public class BiSearch {
	//迭代
	public static int search(int[] a, int key){
		int low = 0;
		int high = a.length-1;
		while(low<=high){
			int mid = low+(high-low)/2;
			if(a[mid]==key)
				return mid;
			else if (a[mid]>key)
				high = mid-1;
			else
				low = mid+1;
		}
		return -1;
	}
	//递归
	public static int searchR(int[] a, int key,int low,int high){
		if(low>high)return -1;
		int mid = low+(high-low)/2;
		if(a[mid]==key)
			return mid;
		if(a[mid]>key)
			return searchR(a, key, low, mid-1);
		return searchR(a,key,mid+1,high);
	}
	
	public static void main(String[] args) {
		int[] a = {1,2,6,10,15,16,17};
		int res = BiSearch.searchR(a, 5,0,a.length-1);
		System.out.println(res);
	}
}
